<?php if(!defined('FRAME')) exit('No direct script access allowed');
/**
 * 路由
 * @author wisoch@hotmail.com
 *
 */
class Route {
	private $_router;
	private $router;
	
	public function rewrite($str) {
		if(empty($this->router)) return false;
		
		foreach($this->router as $key => $value) {
			if(!isset($value['type'])) continue;

			switch($value['type']) {
				case 'normal':
					if(!isset($value['route'])) continue;
					if(is_string($value['route'])) {
						if($str == $value['route']) return $key;
					}
					if(is_array($value['route'])) {
						if(in_array($str, $value['route'])) return $key;
					}
					break;
				case 'regex':
					if(preg_match($value['route'], $str)) {
						$string = preg_replace($value['route'], $value['params'], $str);
						parse_str($string, $GET);
						$_GET = array_merge($_GET, $GET);
						return $key;
					}
					break;
				default:continue;
			}
		}
		return false;
	}
	
	public function addRouter($name, $router) {
		$this->router[$name] = $router;
	}
	
	public function getRouter($router) {
		return isset($this->router[$router])? $this->router[$router]: null;
	}
}